package com.xintonghua.bussiness.api.net;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.api.myinterface.DataCallBack;
import com.xintonghua.bussiness.util.DialogUtils;
import com.xintonghua.bussiness.util.MyUtils;

import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;

/**
 * Created by 王凡 on 2017/9/27.
 */

public class HandleResponseService extends HttpUtils {
    public static HandleResponseService responseService;
    private static Context context;
    private static boolean show = true;

    public static HandleResponseService init(Context mContext) {
        show = true;
        context = mContext;
        if (responseService == null) {
            responseService = new HandleResponseService();
        }
        return responseService;
    }

    public static HandleResponseService init(Context mContext, boolean mShow) {
        context = mContext;
        show = mShow;
        if (responseService == null) {
            responseService = new HandleResponseService();
        }
        return responseService;
    }


    public void get(final RequestParams params, final DataCallBack callBack, final int order, final String interfaceName) {
        if (!MyUtils.isNetworkAvailable(context)) {
            MyUtils.mToast(context, "当前网络不可用");
            return;
        }
        LogUtil.d(JSONObject.toJSONString(params));
        Log.i("aaa", "接口名==>" + interfaceName + "提交的数据==>" + params.getQueryStringParams().toString());//JSONObject.toJSONString(params)
        DialogUtils.showWithStatus(context, show);
        HttpUtils.get(params, new HttpResponseHandler() {
            @Override
            public void onSuccess(NetMessage message) {
                LogUtil.i(message.getData().toString());
                Log.i("aaa", "接口名==>" + interfaceName + "接受到的消息===>" + message.getData().toString());
                if (message.getSuccess()) {
                    callBack.dataBack(message.getData(), order);
                } else {
                    callBack.errorBack(message.getData(), order);
                    MyUtils.mToast(context, message.getMsg());
                }
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("x_log", throwable.getMessage());
                if (!MyUtils.isNetworkAvailable(context)) {
                    MyUtils.mToast(context, "当前网络不可用");
                } else {
                    MyUtils.mToast(context, "获取失败请稍后再试");
                }

            }

            @Override
            public void onFinish() {
                callBack.finishBack();
                DialogUtils.disMissDialog();
            }
        });
    }

    String tag = "HaService";

    public void post(final RequestParams params, final DataCallBack callBack, final int order, final String interfaceName) {
        if (!MyUtils.isNetworkAvailable(context)) {
            MyUtils.mToast(context, "当前网络不可用");
            return;
        }
        LogUtil.i(JSONObject.toJSONString(params));
        Log.i("aaa", "接口名=>" + interfaceName + "提交的数据=》" + params.getQueryStringParams().toString());
        DialogUtils.showWithStatus(context, show);
        HttpUtils.post(params, new HttpResponseHandler() {
            @Override
            public void onSuccess(NetMessage message) {
                LogUtil.i(JSONObject.toJSONString(params));
                LogUtil.i(message.getJson());
                Log.i("aaa", "接口id=>" + interfaceName + "接受到的消息=>" + message.getJson());
                if (message.getSuccess()) {
                    callBack.dataBack(message.getData(), order);
                } else {
                    callBack.errorBack(message.getMsg(), order);
                    MyUtils.mToast(context, message.getMsg());
                }
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("aaa", "onError网络错误" + throwable);
                if (!MyUtils.isNetworkAvailable(context)) {
                    MyUtils.mToast(context, "当前网络不可用");
                } else {
                    MyUtils.mToast(context, "服务器繁忙,请稍后再试");
                }
            }

            @Override
            public void onFinish() {
                callBack.finishBack();
                DialogUtils.disMissDialog();
            }
        });
    }


}
